package self.Lunastic.HexaSweeper.Unit;

import self.Lunastic.HexaSweeper.Base.UpdateLoop;
import self.Lunastic.HexaSweeper.Manager.GameManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;

public class ButtonBase extends JButton {
    // 自动更新对象
    protected UpdateLoop updateLoop = new UpdateLoop(this::update, 1000/GameManager.TPS);
    public void setUpdate(){ this.updateLoop.set();}
    public void startUpdate(){ this.updateLoop.start(); }
    public void stopUpdate(){ this.updateLoop.stop(); }
    public void endUpdate(){ this.updateLoop.end(); }

    // 板机 Map
    private final HashMap<String, Runnable> handlerMap = new HashMap<>();

    /**
     *  部署板机
     * @param handlerName 板机名称
     * @param handler 板机行为
     */
    public void setHandler (String handlerName, Runnable handler){
        this.handlerMap.put(handlerName, handler);
    }

    /**
     * 触发板机
     * @param handlerName 板机名称
     */
    public void doHandler (String  handlerName){
        HashMap<String, Runnable> map = this.handlerMap;
        this.startUpdate();
        if(map.get(handlerName) != null) map.get(handlerName).run();
    }

    protected Graphics2D g2d;
    // 点击状态 （处理多压）
    protected boolean _leftPress = false;
    protected boolean _rightPress = false;
    protected boolean _doublePress = false;

    // 悬停状态
    protected boolean _hover = false;

    public ButtonBase(int x, int y, int w, int h){
        // 清除样式
        this.setBorderPainted(false);
        this.setFocusPainted(false);
        this.setContentAreaFilled(false);
        this.setBounds(x, y, w, h);
        this.setUpdate();

        // 按键事件处理 添加双键判定
        this.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) { }

            @Override
            public void mousePressed(MouseEvent e) {
                if (!isEnabled()) return;
                // 含多压处理
                switch (e.getButton()){
                    case 1:{
                        if (_rightPress) {
                            _doublePress = true;
                            doDoublePressed();
                        }
                        else {
                            doLeftPressed();
                            _leftPress = true;
                        }
                        break;
                    }
                    case 3:{
                        if (_leftPress) {
                            _doublePress = true;
                            doDoublePressed();
                        }
                        else {
                            doRightPressed();
                            _rightPress = true;
                        }
                        break;
                    }
                }
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if (!isEnabled()) return;
                switch (e.getButton()){
                    case 1:{
                        // 当左右键都释放时候才触发 下同
                        if(_hover) if(_doublePress){
                            if(!_rightPress) {
                                _doublePress = false;
                                doDoubleReleased();
                            }
                        }else{
                            doLeftReleased();
                        }
                        _leftPress = false;
                        break;
                    }
                    case 3:{
                        if(_hover) if(_doublePress){
                            if(!_leftPress) {
                                _doublePress = false;
                                doDoubleReleased();
                            }
                        }else{
                            doRightReleased();
                        }
                        if(!_leftPress) _doublePress = false;
                        _rightPress = false;
                        break;
                    }
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                if (!isEnabled()) return;
                _hover = true;
                doEntered();
            }

            @Override
            public void mouseExited(MouseEvent e) {
                if (!isEnabled()) return;
                _hover = false;
                _leftPress = false;
                _rightPress = false;
                _doublePress = false;
                doExited();
            }
        });
    }

    protected void update(){}
    // 绘制事件
    @Override
    protected void paintComponent(Graphics g) {
        g2d = (Graphics2D) g.create();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        this.extraPaint();
        super.paintComponent(g);
    }
    protected void extraPaint(){}
    // 各种鼠标事件 需要根据使用场景进行重构
    protected void doDoublePressed(){ this.doHandler("double pressed"); }
    protected void doDoubleReleased(){ this.doHandler("double released"); }
    protected void doLeftReleased(){ this.doHandler("left released"); }
    protected void doRightReleased(){ this.doHandler("right released"); }
    protected void doLeftPressed(){ this.doHandler("left pressed"); }
    protected void doRightPressed(){ this.doHandler("right pressed"); }
    protected void doEntered(){ this.doHandler("entered"); }
    protected void doExited(){ this.doHandler("exited"); }
}
